/**
 * 
 */
package com.seedwill.common.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.seedwill.common.exception.DataConvertionException;

/**
 * 数值解析帮助
 * @author kdyi
 */
public class NumberHelper {
	
	private static Logger logger = Logger.getLogger(NumberHelper.class);

	private static final String zeroStr = "0";
	private static final String emptyStr = "";

	
	/**
	 * 把null转为0
	 * @param num
	 * @return
	 */
	public static Number null2Zero(Number num){
		if(null == num){
			return 0;
		}else{
			return num;
		}
	}
	public static String null2ZeroStr(Number num){
		if(null == num){
			return zeroStr;
		}else{
			return ""+num;
		}
	}
	public static String formatInteger(Double num) {
		String str = emptyStr;
		if(null != num){
				NumberFormat nf = java.text.NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(0);
				str = nf.format(num);
		}
		return str;
	}
	
	public static String formatInteger(Double num, boolean emptyToZero) {
		String str = emptyStr;
		if(null != num){
				NumberFormat nf = java.text.NumberFormat.getNumberInstance();
				nf.setMaximumFractionDigits(0);
				str = nf.format(num);
		}else{
			if(emptyToZero){
				str = zeroStr;
			}
		}
		return str;
	}
	
	public static String formatNumber(Number num, int dLen) {
		String str = emptyStr;
			NumberFormat nf = java.text.NumberFormat.getNumberInstance();
			nf.setMaximumFractionDigits(dLen);
			str = nf.format(num);
		return str;
	}
	
	public static String formatDouble(double num, int dLen) {
		String str = emptyStr;
			NumberFormat nf = java.text.NumberFormat.getNumberInstance();
			nf.setMaximumFractionDigits(dLen);
			str = nf.format(num);
		return str;
	}
	
	public static String formatLong(long num, int dLen) {
		String str = emptyStr;
			NumberFormat nf = java.text.NumberFormat.getNumberInstance();
			nf.setMaximumFractionDigits(dLen);
			str = nf.format(num);
		return str;
	}
	
	/**
	 * 解析为double
	 * @param obj
	 * @return
	 * @throws DataConvertionException
	 */
	public static double object2Double(Object obj) throws DataConvertionException {
		return object2Double(obj, false);
	}
	
	/**
	 * 解析为double
	 * @param obj
	 * @param emptyToZero 是否把null或空转为0
	 * @return
	 * @throws DataConvertionException
	 */
	public static double object2Double(Object obj, boolean emptyToZero) throws DataConvertionException {
		double val = 0;
		//
		if(emptyToZero && (null == obj || (0 == obj.toString().trim().length()))){
			val = 0;
		}else{
			//Assert.notNull(obj);
			if(obj!=null){
				if (obj instanceof Double) {
					val = (Double) obj;
				} else {
					try {
						val = Double.parseDouble(obj.toString());
					} catch (NumberFormatException e) {
							logger.debug("number format" , e);
							throw new DataConvertionException(e);
					}
				}				
			}

		}
		return val;
	}
	
	/**
	 * 解析为int
	 * @param obj
	 * @return
	 * @throws DataConvertionException
	 */
	public static int object2Int(Object obj) throws DataConvertionException {
		return object2Int(obj, false);
	}
	
	/**
	 * 解析为int
	 * @param obj
	 * @param emptyToZero 是否把null或空转为0
	 * @return
	 * @throws DataConvertionException
	 */
	public static int object2Int(Object obj, boolean emptyToZero) throws DataConvertionException {
		int val = 0;
		//Assert.notNull(obj);
		if(emptyToZero && (null == obj || (0 == obj.toString().trim().length()))){
			val = 0;
		}else{
			//Assert.notNull(obj);
			// 暂时只支持INT, LONG的直接转换，其他通过PARSE
			if(obj!=null){
				if (obj instanceof Integer) {
					val = (Integer) obj;
				}else if (obj instanceof Number) {
					val = ((Number) obj).intValue();
				}  else {
					try {
						val = Integer.parseInt(obj.toString());
					} catch (NumberFormatException e) {
						logger.debug("number format" , e);
						throw new DataConvertionException(e);
					}
				}
			}
		}
		return val;
	}
	
	public static Long[] parseArrayLong(String[] arrStr) throws DataConvertionException{
		Long[] arr = null;
		if(null != arrStr && arrStr.length>0){
			try {
				arr = new Long[arrStr.length];
				for(int i=0; i<arrStr.length; i++){
					arr[i] = object2Long(arrStr[i], false);
				}
			} catch (DataConvertionException e) {
				throw e;
			}
		}
		return arr;
	}
	
	/**
	 * 解析为long
	 * @param obj
	 * @param emptyToZero 是否把null或空转为0
	 * @return
	 * @throws DataConvertionException
	 */
	public static long object2Long(Object obj, boolean emptyToZero) throws DataConvertionException {
		long val = 0;
		//Assert.notNull(obj);
		if(emptyToZero && (null == obj || (0 == obj.toString().trim().length()))){
			val = 0;
		}else{
			//Assert.notNull(obj);
			// 暂时只支持INT, LONG的直接转换，其他通过PARSE
			if(obj!=null){
				if (obj instanceof Long) {
					val = (Long) obj;
				} else {
					if (obj instanceof Integer) {
						val = Long.valueOf((Integer)obj);
					} else {
						try {
							val = Long.parseLong(obj.toString());
						} catch (NumberFormatException e) {
							logger.debug("number format" , e);
								throw new DataConvertionException(e);
						}
					}
				}				
			}

		}
		return val;
	}
	
	/**
	 * 解析为long
	 * @param obj
	 * @return
	 * @throws DataConvertionException
	 */
	public static long object2Long(Object obj) throws DataConvertionException {
		return object2Long(obj, false);
	}

	/**
	 * 解析为BigDecimal
	 * @param para
	 * @return
	 * @throws NumberFormatException
	 */
	public static BigDecimal parseAsDecimal(String para) throws NumberFormatException{
		BigDecimal d = null;
		if(ParaCheckHelper.validParaStrTrimed(para)){
			try {
				d = BigDecimal.valueOf(Double.parseDouble(para));
			} catch (NumberFormatException e) {
				throw e;
			}
		}
		return d;
	}

	/**
	 * 解析为Double
	 * @param para
	 * @return
	 * @throws NumberFormatException
	 */
	public static Double parseAsDouble(String para)  throws NumberFormatException{
		Double d = null;
		if(ParaCheckHelper.validParaStrTrimed(para)){
			try {
				d = Double.parseDouble(para);
			} catch (NumberFormatException e) {
				throw e;
			}
		}
		return d;
	}

	/**
	 * 解析为Integer
	 * @param para
	 * @return
	 * @throws NumberFormatException
	 */
	public static Integer parseAsInteger(String para) throws NumberFormatException {
		Integer d = null;
		if(ParaCheckHelper.validParaStrTrimed(para)){
			try {
				d = Integer.parseInt(para);
			} catch (NumberFormatException e) {
				throw e;
			}
		}
		return d;
	}

	/**
	 * 解析为LONG
	 * @param para
	 * @return
	 * @throws NumberFormatException
	 */
	public static Long parseAsLong(String para) throws NumberFormatException {
		Long d = null;
		if(ParaCheckHelper.validParaStrTrimed(para)){
			try {
				d = Long.parseLong(para);
			} catch (NumberFormatException e) {
				d = null;
				throw e;
			}
		}
		return d;
	}
	public static Long parseAsLongSlient(String para) {
		Long d = null;
		if(ParaCheckHelper.validParaStrTrimed(para)){
			try {
				d = Long.parseLong(para);
			} catch (NumberFormatException e) {
				d = null;
			}
		}
		return d;
	}
	/**
	 * @param i
	 * @return
	 */
	public static String integer2Str(int i){
		return ""+i;
	}
	public static List<Serializable> parseListLong(
			String formListIgnoreListContStr, String delimeter) {
		List<Serializable> list = null;
		if(formListIgnoreListContStr!=null && delimeter!=null){
			String[] args = formListIgnoreListContStr.split(delimeter);
			if(args!=null && args.length>0){
				list = new ArrayList<Serializable>();
				for(String s : args){
					Long l = parseAsLongSlient(s);
					if(l!=null){
						list.add(l);
					}
					
				}
				
			}
		}
		return list;
	}
 

}
